<template>
	<view class="wrap">
		<image src="../../static/images/bg.png" class="bg"></image>
		<view class="form_box">
			<view class="form_item">
				<input type="text" v-model="teacherPhone" placeholder="请输入手机号码" class="ipt flex1"/>
			</view>
			<view class="form_item">
				<input type="text" v-model="verifyCode" placeholder="请输入验证码" class="ipt flex1"/>
				<view @tap="getCode" class="getcode">{{t>0?t+'s后重新发送':'获取验证码'}}</view>
			</view>
		</view>
		<view class="btn_box">
			<button type="default" class="btn" :loading='loading' @tap="login">登录</button>
		</view>
		<view class="agree">登录代表您已同意<text>家庭辅导师用户协议、隐私协议</text></view>
	</view>
</template>

<script>
	import {toast,getUrlParam} from '@/common/js/public.js'
	export default {
		data() {
			return {
				appId:'wx08c19827d10b78fe',
				secret:'4bd80cc7b1e7dc320b4e5323fb4b01f4',
				grant_type:'authorization_code',
				openId:'',
				code:'',
				teacherUnionId:'',
				loading:false,
				teacherPhone:'',
				uuid:'',
				verify:'',
				verifyCode:'',
				t:0,
				type:''
			}
		},
		onLoad(option) {
			this.type = option.type || 'index'
			this.wxAuth()
			// this.wxGetOpenId()
		},
		methods: {
			// 微信授权
			wxAuth() {
				let code = getUrlParam('code');
				if (!code) {
					this.wxjump(window.location.href)
				} else {
					uni.showLoading({
					  title: '加载中...'
					});
					this.code = code
					this.wxGetOpenId()
				}
			},
			wxjump(fromurl) {
				let redirect_uri = encodeURIComponent(fromurl)
				// let redirect_uri = encodeURIComponent('https://jiajiao.xiaozile.cn')
				var url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+this.appId+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
				console.log(url)
				location.href=url;
			},
			// 微信获取openID
			wxGetOpenId() {
				let param = {
					"appid": this.appId,
					"grantType": this.grant_type,
					"jsCode": this.code,
					"secret": this.secret
				}
				this.$api.queryTeacherLoginOpenId(param).then(res=>{
					uni.hideLoading();
					if (res.code == 1) {
						this.openId = res.data.openid
					} else {
						toast(res.msg)
					}
				})
			},
			login() {
				var myreg=/^1[0-9]{10}$/
				if (!myreg.test(this.teacherPhone)) {
					toast('手机号格式不正确')
					return;
				}
				if (this.uuid == '') {
					toast('请先发送验证码')
					return
				}
				if (this.verifyCode=='' || this.verifyCode != this.verify) {
					toast('验证码不正确')
					return
				}
				if(this.openId == '') {
					uni.showModal({
						title: '提示',
						content: '请先授权',
						success: (res) => {
							if (res.confirm) {
								this.wxjump(window.location.href)
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					return
				}
				if (this.loading) return
				this.loading = true
				uni.showLoading({
				    title: '登陆中'
				});
				let param = {
					"code": this.verifyCode,
					"codeUuid": this.uuid,
					"teacherAppId": this.appId,
					"teacherOpenId": this.openId,
					"teacherPhone": this.teacherPhone,
					"teacherUnionId":this.teacherUnionId
				}
				this.$api.teacherLogin(param).then(res=>{
					uni.hideLoading();
					if(res.code == 1) {
						this.loading = false
						let info = {
							teacherNo:res.data.teacherNo,
							"teacherAppId": this.appId,
							"teacherOpenId": this.openId,
							"teacherUnionId": this.teacherUnionId
						}
						uni.setStorageSync('teacher_userinfo',info)
						if (this.type == 'index') {
							uni.reLaunch({
								url:'/pages/index/index'
							})
						} else if (this.type == 'myclass') {
							uni.reLaunch({
								url:'/pages/myClass/myClass'
							})
						} else {
							uni.reLaunch({
								url:'/pages/joinAdd/joinAdd'
							})
						}
					} else {
						toast(res.msg)
						this.loading = false
					}
				})
			},
			// 发送验证码
			getCode(){
				if (this.t>0) return
				uni.hideKeyboard()
				var myreg=/^1[0-9]{10}$/
				if (!myreg.test(this.teacherPhone)) {
					toast('手机号格式不正确')
					return;
				}
				let param = {
					"teacherAppId": this.appId,
					"teacherOpenId": this.openId,
					"teacherPhone": this.teacherPhone,
					"teacherUnionId": this.teacherUnionId
				}
				this.$api.queryTeacherLoginVerificationCode(param).then(res=>{
					if(res.code==1){
						this.t = 60
						this.uuid = res.data.uuid
						this.verify = res.data.code
						setTimeout(() => {
							this.countdown()
						}, 1000)
					}else{
						toast(res.message)
					}
				})
			},
			// 倒计时
			countdown(){
				if(this.t > 0){
					this.t--
				}else{
					return
				}
				setTimeout(() => {
					this.countdown()
				}, 1000)
			},
		}
	}
</script>

<style lang="stylus" scoped>
	.wrap
		width 100%
		height 100vh
		position relative
		display flex
		flex-direction column
	.bg
		width 100%
		height 310rpx
	.form_box
		flex 1
		min-height 0
		padding 0 76rpx
		margin 40rpx 0
		box-sizing border-box
		.form_item
			padding 20rpx 0
			margin-bottom 16rpx
			border-bottom 2rpx solid #eee
			display flex
			align-items center
			.ipt
				font-size 26rpx
				color #333
			.getcode
				font-size 26rpx
				color #34BE97
	.btn_box
		width 100%
		box-sizing border-box
		padding 0 60rpx
		.btn
			width 100%
			height 88rpx
			line-height 88rpx
			background-color #34BE97
			color #fff
			font-size 28rpx
			border none
			border-radius 88rpx
	.tips
		font-size 24rpx
		color #999
	.agree
		font-size 26rpx
		color #999
		width 100%
		padding 40rpx 0
		text-align center
		text
			color #34BE97
</style>
